#coding=utf-8
import datetime

from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db import transaction
from django.shortcuts import render_to_response, redirect
from django.contrib.auth.decorators import login_required

from accounts.forms import LoginForm, RegistrationForm
from accounts.models import UserProfile

import re

field_re = re.compile(r'[\w]+')

def account_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            user = authenticate(username=request.POST['username'], password=request.POST['password'])
            login(request, user)
            return redirect(reverse('login_index'))
    else:
        if not request.user.is_anonymous():
            return redirect(reverse('account_login'))
        else:
            form = LoginForm()
    return render_to_response('accounts/not_login_index.html',
                              {'form': form})

def account_logout(request):
    logout(request)
    return redirect(reverse('account_login'))

@transaction.commit_on_success
def account_register(request):
    errors, msn, qq, address, company, celphone_num, student_num = '', '', '', '', '', '', ''
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data["username"]
            password = form.cleaned_data["password"]
            email = form.cleaned_data['email']
            msn = form.cleaned_data['msn']
            qq = form.cleaned_data['qq']
            name = form.cleaned_data['name']
            address = form.cleaned_data['address']
            remarks = form.cleaned_data['remarks']
            company = form.cleaned_data['company']
            student_num = form.cleaned_data['student_num']
            celphone_num = form.cleaned_data['celphone']
            user = User.objects.create_user(username, email, password=password)
            UserProfile.objects.create(user=user,
                                       company=company,
                                       qq=qq,
                                       celphone_num=celphone_num,
                                       student_num=student_num,
                                       msn=msn,
                                       address=address,
                                       name=name,
                                       remarks=remarks,
                                       limit=u'子账户',
                                       last_login_time=datetime.datetime.now(),
                                       created_time=datetime.datetime.now(),
                                       nickname='')
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect(reverse('login_index'))
        else:
            errors = form.errors
    else:
        form = RegistrationForm()
    return render_to_response('accounts/registration.html', {'form': form, 'errors':errors})

